package cn.zysoft.his4killsession.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.zysoft.his4killsession.pojo.UserInfo;

/***
 * @author QSJ
 * @date 2017-7-6
 * @describe 拦截器
 */
public class SpringMVCInterceptor implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		//URL:login.jsp是公开的;这个demo是除了login.jsp是可以公开访问的，其它的URL都进行拦截控制 
		String url = request.getRequestURI();
		//获取session中的数据
		UserInfo user = (UserInfo) request.getSession().getAttribute("user");
		if(url.indexOf("login.do")>0){
			return true;
		}if(url.indexOf("tologins.do")>0){
			return true;
		}
		if(url.indexOf("createImg.do")>0){
			return true;
		}
		if(user!=null){
			return true;
		}
		 response.sendRedirect("../login/login.do");
		return false;
	}
	
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object obj, Exception e)
			throws Exception {
		
	}
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object obj, ModelAndView model)
			throws Exception {
		
	}
}
